<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>伪网站-修改密码</title>
  <link rel="stylesheet" href="../CSS/修改密码.css">
</head>


<body>
  <h1>修改密码
    <!-- <p class="p1"><a href="">修改密码</a></p> -->
    <p class="p2"><a href="./首页.html">回到首页</a></p>
  </h1>
  <form>
    <label>原始密码：<input class="oldPassword" type="password"></label>
    <label>新密码：<input class="newPassword" type="password"></label>
    <label>确认新密码：<input class="rNewPassword" type="password"></label>
    <button>确认修改</button>
  </form>
</body>
<script src="../axios.min.js"></script>
<script src="../http.js"></script>
<script>
    // 立即执行函数判断是否登录
    (async function(){
    let r = await isLogin();
    if(r.status != 1){
      location.href = './登录.html';
      return;
  }})();
  const form = document.querySelector('form');
  const oldPassword = document.querySelector('.oldPassword');
  const newPassword = document.querySelector('.newPassword');
  const rNewPassword = document.querySelector('.rNewPassword');

  // 封装一个函数来用提交修改密码的发送请求
  async function pwd() {
    if (oldPassword.value === '' || newPassword.value === '' || rNewPassword.value === '') {
      alert('请填写完整信息');
      return;
    }
    if (newPassword.value !== rNewPassword.value) {
      alert('两次密码不一致');
      return;
    }
    // 立即执行函数判断是否登录
  let id = localStorage.getItem('uid')
    let res = await http.post('/users/rpwd', { id:id, oldPassword: oldPassword.value, newPassword: newPassword.value, rNewPassword: rNewPassword.value })
    if (res.data.code == 1) {
      alert(res.data.message);
      location.href = './登录.html';
    } else {
      alert(res.data.message);
    }
  }
  form.addEventListener('submit', async function (e) {
    e.preventDefault();
    await pwd();
  }
  )


</script>

</html>